package 尚硅谷;

import java.util.HashMap;
import java.util.WeakHashMap;

/**
 * @author zhangmin
 * @create 2022-03-26 10:26
 */
public class WeekHashMapDemo {
    private static void myHashMap(){
        HashMap<Integer,String> map=new HashMap<>();
        Integer key=new Integer(1);
        String value="HashMap";

        map.put(key,value);
        System.out.println(map);
        key=null;//此处的key是这个方法的局部变量，与map无关
        System.out.println(map);

        System.gc();
        System.out.println(map+"\t"+map.size());
    }
    private static void myWeakHashMap(){
        WeakHashMap<Integer,String> map=new WeakHashMap<>();
        Integer key=new Integer(2);
        String value="weakHashMap";

        map.put(key,value);
        System.out.println(map);

        key=null;//此处的key是这个方法的局部变量，与map无关
        System.out.println(map);

        System.gc();
        System.out.println(map+"\t"+map.size());
    }
    public static void main(String[] args) {
        myHashMap();
        System.out.println("========================================");
        myWeakHashMap();
    }
}
